<?php

namespace Admin\Controller;

class OrderController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Order");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        $this->assign("begin_time", $_REQUEST["o#create_time-egt"]);
        $this->assign("end_time", $_REQUEST["o#create_time-elt"]);

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取order列表
        $orderList = $this->currentModel->queryForList($page, $requestMap);
        $this->assign("defaultEnum", C("INVOICE_TYPE"));
        $this->assign("statusEnum", C("ORDER_STATUS"));
        $this->assign('orderList', $orderList);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function viewMeasureData() {

        $id = $_REQUEST["id"];
        $order = $this->currentModel->queryById($id);

        $measureData = json_decode(html_entity_decode($order["measure_data"]) ,true);

        $this->assign("measureData", $measureData);
        $this->display();

    }

    public function endDelivery() {

        $id = $_REQUEST["id"];
        $order = $this->currentModel->queryById($id);

        $this->assign("order", $order);
        $this->display();

    }

    public function end() {

        $id = $_REQUEST["id"];

        $order = $this->currentModel->queryById($id);

        $order["status"] = C("ORDER_STATUS.END_DELIVERY");
        $order["delivery_number"] = $_REQUEST["deliveryNumber"];
        $order["express_company"] = $_REQUEST["expressCompany"];
        $order["outstock_time"] = getCurrentDate();

        $this->currentModel->updateInfo($order);

        //推送留言信息到销售
        $customer = D("Customer")->queryById($order["customer_id"]);
        $product = D("Product")->queryById($order["product_id"]);

        $notifyData["openid"] = $customer["openid"];
        $notifyData["first"] = "您的订单已发货：产品名称[".$product["product_name"]."], 共 ".$order["number"]." 件";
        $notifyData["keyword1"] = $_REQUEST["expressCompany"];
        $notifyData["keyword2"] = $_REQUEST["deliveryNumber"];
        $notifyData["remark"] = "如有疑问，请点击【服务】→【留言】，我们将尽快联系您。";
        orderEndNotify($notifyData);

        $this->success("已标记订单为已出库状态", U("order/index"));

    }

    public function receiveMoney() {

        $id = $_REQUEST["id"];
        $order = $this->currentModel->queryById($id);

        $this->assign("order", $order);
        $this->display();

    }

    public function receive() {

        $id = $_REQUEST["id"];

        $order = $this->currentModel->queryById($id);

        $order["status"] = C("ORDER_STATUS.WAITING_HANDLE");
        $order["receipt_amount"] = $_REQUEST["receiptAmount"];
        $order["receipt_number"] = $_REQUEST["receiptNumber"];

        $this->currentModel->updateInfo($order);

        //推送预约信息到管理员
        $pay = D("Pay")->queryById($order["pay_id"]);
        $customer = D("Customer")->queryById($order["customer_id"]);
        $product = D("Product")->queryById($order["product_id"]);
        $orderSuccessData["title"] = $customer["nickname"]
            ."刚刚购买并支付了".$order["number"]."件".$product["product_name"]."，请核对信息后及时安排相关事项";
        $orderSuccessData["orderNumber"] = $pay["out_trade_no"];
        $orderSuccessData["price"] = $pay["money"]."元";
        $orderSuccessData["remark"] = "希卫康系统自动推送";
        orderSuccessNotify($orderSuccessData);

        $this->success("已标记订单为已付款状态", U("order/index"));

    }
}